<?php

namespace app\api\controller;
use addons\shopro\exception\Exception;
use addons\shopro\library\Redis;
use addons\shopro\model\User;
use addons\shopro\model\UserSign;
use app\common\model\UserScoreRank;
use think\Db;
use think\db\Expression;

class Score extends \app\common\controller\Api
{
    protected $noNeedLogin = ['rank','executeRank','rankList'];
    protected $noNeedRight = '*';
    //积分排行
    public function rank($type='online'){
        $query=User::field('id,nickname,online_score,offline_score,level,score');
        if($type=='online'){
            $query->order('online_score','desc');
        }else{
            $query->order('offline_score','desc');
        }
        $this->success('积分排行',$query->paginate());
    }
    //排行榜
    public function rankList($tyep='online'){
        $list=UserScoreRank::with(['user'])->where('type',$tyep)->order('country_rank')->paginate();
        $this->success('排行榜',$list);
    }


    //我的积分
    public function my(){
        $user=$this->auth->getUser();
        $this->success('我的积分',$user);
    }

    //签到获取积分
    public function sign(){
        $user=$this->auth->getUser();
        $sign = Db::transaction(function () use ($user) {
            $sign = UserSign::where('user_id', $user->id)->where('date', date('Y-m-d'))->lock(true)->find();
            if ($sign) {
                $this->error('您今天已经签到，明天再来吧');
            }
            $time=time();
            // 插入签到记录
            $sign = UserSign::create([
                'user_id' => $user->id,
                'date' => date('Y-m-d', $time),
                'score' => config('site.sign_score'),
                'is_replenish' => 0
            ]);
            if (config('site.sign_score') > 0) {
                User::score(config('site.sign_score'), $user->id, 'sign', $sign->id, '', [
                    'date' => date('Y-m-d', $time)
                ]);
            }
            return $sign;
        });
        $this->success('签到成功',$sign);

    }

    public function executeRank($type='online'){
        $users=\app\common\model\User::where('score','>',0)->field('id,'.$type.'_score as score,province,city')->order('score','desc')->select();
        Db::name('user_score_rank')->where('type',$type)->delete();
        Db::name('user_score_rank')->insertAll($this->perchRank($users,$type));
        $this->success('success');
    }
    //占位
    public function perchRank($users,$type='online'){
        $data=[];
        $rank=1;
        $city_user=[];
        $province_user=[];
        foreach ($users as $key=>$user){
            $data[$key]['user_id']=$user['id'];
            $data[$key]['score']=$user['score'];
            $data[$key]['type']=$type;
            if($key >0 && $user['score']==$users[$key-1]['score']){
                $data[$key]['country_rank']=$data[$key-1]['country_rank'];
            }else{
                $data[$key]['country_rank']=$rank;
            }

            if(!empty($province_user[$user['province']]) && $user['score'] ==$province_user[$user['province']][count($province_user[$user['province']])-1]['score']){
                $data[$key]['province_rank']=$province_user[$user['province']][count($province_user[$user['province']])-1]['province_rank'];
            }else{
                $data[$key]['province_rank']=empty($province_user[$user['province']]) ? 1 : count($province_user[$user['province']])+1;
            }
            $province_user[$user['province']][]=$data[$key];

            if(!empty($city_user[$user['province'].$user['city']]) && $user['score'] ==$city_user[$user['province'].$user['city']][count($city_user[$user['province'].$user['city']])-1]['score']){
                $data[$key]['city_rank']=$city_user[$user['province'].$user['city']][count($city_user[$user['province'].$user['city']])-1]['city_rank'];
            }else{
                $data[$key]['city_rank']=empty($city_user[$user['province'].$user['city']]) ? 1 : count($city_user[$user['province'].$user['city']])+1;
            }
            $city_user[$user['province'].$user['city']][]=$data[$key];
            $rank++;
        }
        return $data;
    }
    //不占位
    public function noPerchRank($users,$type){
        $data=[];
        $rank=1;
        $city_rank=[];
        $city_user=[];
        foreach ($users as $key=>$user){
            $data[$key]['user_id']=$user['id'];
            if($key >0 && $user['score']==$users[$key-1]['score']){
                $data[$key][$type.'_rank']=$data[$key-1][$type.'_rank'];
            }else{
                $data[$key][$type.'_rank']=$rank;
                $rank++;
            }
            $city_user[$user['province'].$user['city']][]=$user;

            if($key >0 && $user['score']==$users[$key-1]['score']){
                $data[$key][$type.'_rank']=$data[$key-1][$type.'_rank'];
            }else{
                $data[$key][$type.'_rank']=$rank;
                $rank++;
            }
        }
        return $city_user;
    }


}